<?php
//=========== the following lines are required so that the sub theme can work  =====================================
// remove_action('thesis_hook_header', 'thesis_default_header');
remove_action('thesis_hook_before_header', 'thesis_nav_menu');
// add_action('thesis_hook_before_content_box', 'thesis_default_header');
add_action('thesis_hook_after_header', 'thesis_nav_menu');
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
remove_action('thesis_hook_404_title', 'thesis_404_title');
remove_action('thesis_hook_404_content', 'thesis_404_content');
remove_action('thesis_hook_footer', 'thesis_attribution');
//=========== end of lines ================================================================================
$pathToImageFolder = getImagePath(__FILE__);

//.............................................................................
//	THIS IS WHAT I CONSIDER 'FUNCTIONS.PHP' CODE

//.............................................................................
// Homepage - Contact, Hours, Location

register_sidebar(array('name'=>'Homepage - Contact, Hours, Location', 'id'=>'homepage-contact-hours-location', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));

function homepage_contact_hours_location(){
	?>
		<div class="homepage-contact-hours-location">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Contact, Hours, Location') ){ ?>
				<ul><li class="widget">Dynamic Widgets are not enabled</li></ul>
			<?php } ?>
		</ul>		
	<?php
}

//.............................................................................
// Homepage - Menus

register_sidebar(array('name'=>'Homepage - Menus', 'id'=>'homepage-menus', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));

function homepage_menus(){
	?>
		<div class="homepage-menus">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Menus') ){ ?>
				<ul><li class="widget">Dynamic Widgets are not enabled</li></ul>
			<?php } ?>
		</ul>		
	<?php
}

//.............................................................................
// Homepage - Footer Location 01

register_sidebar(array('name'=>'Homepage - Footer Location 01', 'id'=>'homepage-footer-01', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));

function homepage_footer_01(){
	?>
		<div class="homepage-footer-01">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Footer Location 01') ){ ?>
				<ul><li class="widget">Dynamic Widgets are not enabled</li></ul>
			<?php } ?>
		</ul>		
	<?php
}

//.............................................................................
// Homepage - Footer Location 02

register_sidebar(array('name'=>'Homepage - Footer Location 02', 'id'=>'homepage-footer-02', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));

function homepage_footer_02(){
	?>
		<div class="homepage-footer-02">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Footer Location 02') ){ ?>
				<ul><li class="widget">Dynamic Widgets are not enabled</li></ul>
			<?php } ?>
		</ul>		
	<?php
}

//.............................................................................
// Homepage - Footer Location 03

register_sidebar(array('name'=>'Homepage - Footer Location 03', 'id'=>'homepage-footer-03', 'before_title'=>'<h2>', 'after_title'=>'</h2>'));

function homepage_footer_03(){
	?>
		<div class="homepage-footer-03">
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage - Footer Location 03') ){ ?>
				<ul><li class="widget">Dynamic Widgets are not enabled</li></ul>
			<?php } ?>
		</ul>		
	<?php
}

//.............................................................................
//.............................................................................
// HOME PAGE CUSTOM TEMPLATE

function home_pagecustom() {
if (is_home() || is_front_page())  {
?>

<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
											
<div id="home_top_message">
	<div id="home_message">
		<?php the_content( ); ?>
	</div>
	<?php homepage_contact_hours_location(); ?>
	<?php homepage_menus(); ?>
</div>

<?php endwhile; endif; ?>

<div id="home_bottom_content_row">
	<?php homepage_footer_01(); ?>
	<?php homepage_footer_02(); ?>
	<?php homepage_footer_03(); ?>
</div>

<?php } }

/* HOME PAGE IMPLEMENT */
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'home_pagecustom');

//.............................................................................
//.............................................................................
//.............................................................................
//	BEGIN Custom Post Type 'Menu Item'

function create_menuitem() {
    $menuitem_args = array(
        'label' => __('Menu Items'),
        'singular_label' => __('Menu Item'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'taxonomies' => array('post_tag', 'category'),
        'rewrite' => array( 'slug' => 'menuitem', 'with_front' => false ),
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'comments', 'author')
    );
    register_post_type('menuitem',  $menuitem_args);
}
add_action('init', 'create_menuitem');

// END Custom Post Type 'Menu Item'

//..............................................................................// BEGIN Adding Thesis Meta Boxes to 'Menu Items' */

add_action('admin_menu','my_admin_menu');
function my_admin_menu() {
	$post_options = new thesis_post_options;
	$post_options->meta_boxes();
		foreach ($post_options->meta_boxes as $meta_name => $meta_box)
		{
			add_meta_box($meta_box['id'], $meta_box['title'], array('thesis_post_options', 'output_' . $meta_name . '_box'), 'menuitem', 'normal', 'high'); #wp
		}
add_action('save_post', array('thesis_post_options', 'save_meta')); #wp
}

// END Adding Thesis Meta Boxes to 'Menu Items'


//.............................................................................
// BEGIN Custom Columns for 'Menu Items'

function menuitem_edit_columns($menuitem_columns){
	$menuitem_columns = array(
		"cb" => "<input type=\"checkbox\" />",
		"title" => "Menu Item Title",
		"description" => "Description",
	);
return $menuitem_columns;
}

function menuitem_columns_display($menuitem_columns){
	switch ($menuitem_columns)
	{
	case "description":
		the_excerpt();
		break;
	}
}
add_filter('manage_edit-menuitem_columns', 'menuitem_edit_columns');
add_action('manage_posts_custom_column',  'menuitem_columns_display');

// END Custom Columns for 'Menu Items'



//.............................................................................
//	BEGIN 'Menu' Post Queries to display on page

function custom_page_type_menu() {
if (is_page('menu')) { ?>
	<div id="sidebars">
	<?php thesis_build_sidebars(); ?>
	</div>
		<div id="content" class="hfeed">
	<?php 
		query_posts(array('post_type' => 'menuitem', 'posts_per_page' => 10));
			 thesis_loop::single();
	?>
		</div>
<?php }
}
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');  
add_action('thesis_hook_custom_template', 'custom_page_type_menu');

// END 'Menu' Post Queries



//.............................................................................
//	BEGIN Custom Fields Meta Box

$prefix = 'dwd_';

$meta_box = array(
	'id' => 'menuitemmeta1',
	'title' => 'Menu Item Details',
	'page' => 'menuitem',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array(
			'name' => 'Price',
			'desc' => 'Set the price for the item',
			'id' => $prefix . 'price',
			'type' => 'text',
			'std' => '$5.00'
		),
array(
			'name' => 'House Specialty',
			'id' => $prefix . 'specialty',
			'type' => 'checkbox'
		),
array(
			'name' => 'Vegetarian',
			'id' => $prefix . 'vegetarian',
			'type' => 'checkbox'
		),
array(
			'name' => 'Vegan',
			'id' => $prefix . 'vegan',
			'type' => 'checkbox'
		),
array(
			'name' => 'Spicy',
			'id' => $prefix . 'spicy',
			'type' => 'checkbox'
		)
//		array(
//			'name' => 'Textarea',
//			'desc' => 'Enter big text here',
//			'id' => $prefix . 'textarea',
//			'type' => 'textarea',
//			'std' => 'Default value 2'
//		),
//		array(
//			'name' => 'Select box',
//			'id' => $prefix . 'select',
//			'type' => 'select',
//			'options' => array('Option 1', 'Option 2', 'Option 3')
//		),
//		array(
//			'name' => 'Radio',
//			'id' => $prefix . 'radio',
//			'type' => 'radio',
//			'options' => array(
//				array('name' => 'Name 1', 'value' => 'Value 1'),
//				array('name' => 'Name 2', 'value' => 'Value 2')
//			)
//		),
	)
);

add_action('admin_menu', 'mytheme_add_box');

// Add meta box
function mytheme_add_box() {
	global $meta_box;
	
	add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}

// Callback function to show fields in meta box
function mytheme_show_box() {
	global $meta_box, $post;
	
	// Use nonce for verification
	echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
	
	echo '<table class="form-table">';

	foreach ($meta_box['fields'] as $field) {
		// get current post meta data
		$meta = get_post_meta($post->ID, $field['id'], true);
		
		echo '<tr>',
				'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
				'<td>';
		switch ($field['type']) {
			case 'text':
				echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
					'<br />', $field['desc'];
				break;
			case 'textarea':
				echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
					'<br />', $field['desc'];
				break;
			case 'select':
				echo '<select name="', $field['id'], '" id="', $field['id'], '">';
				foreach ($field['options'] as $option) {
					echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
				}
				echo '</select>';
				break;
			case 'radio':
				foreach ($field['options'] as $option) {
					echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
				}
				break;
			case 'checkbox':
				echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
				break;
		}
		echo 	'<td>',
			'</tr>';
	}
	
	echo '</table>';
}

add_action('save_post', 'mytheme_save_data');

// Save data from meta box
function mytheme_save_data($post_id) {
	global $meta_box;
	
	// verify nonce
	if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
		return $post_id;
	}

	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}

	// check permissions
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post_id;
		}
	} elseif (!current_user_can('edit_post', $post_id)) {
		return $post_id;
	}
	
	foreach ($meta_box['fields'] as $field) {
		$old = get_post_meta($post_id, $field['id'], true);
		$new = $_POST[$field['id']];
		
		if ($new && $new != $old) {
			update_post_meta($post_id, $field['id'], $new);
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $field['id'], $old);
		}
	}
}

//.............................................................................
//	END Custom Fields Meta Box

?>